# -*- coding : utf-8 -*-
# @author    : gaozhigang
# @datetime  : 2023/2/3  16:48
# @file      : random_str.py
# @Software  : Pycharm


class GetRandomStr(object):
    def __init__(self, length: int, str_list: str):
        """
        返回一组字符的所有组合。
        返回一个迭代器对象，需要传入所需的长度，以及组成元素
        :param length: 需要生成的的长度
        :param str_list: 由那些字符组成 例如 （1,2,3,4）
        """
        if not isinstance(length, int):
            raise TypeError("length:请初始化为数值型")
        if isinstance(str_list, str):
            str_list = list(str_list)
        if not isinstance(str_list, list):
            raise TypeError("str_list:请初始化为列表或者字符串类型")
        self.length = length
        self.str_list = str_list

    def __iter__(self):
        self.subscript = 0
        return self

    def __next__(self):
        password = ''
        dijia = self.subscript
        for i in range(self.length):
            l1 = dijia % len(self.str_list)
            password += self.str_list[l1]
            dijia = dijia // len(self.str_list)
        if self.subscript == len(self.str_list) ** self.length:
            raise StopIteration
        self.subscript += 1
        return password


# 调用示例
for info in GetRandomStr(2, "1234"):
    print(info)
